package com.my.hp_hr.dao;

import com.my.hp_hr.entity.Department;
import com.my.hp_hr.util.BaseDAO;

import java.util.List;

public class DepartmentDao extends BaseDAO {
    public List<Department> departmentList() throws Exception {
        String sql ="select * from department";
        List<Department> list = getList(sql, new Department(), new Object[0]);
        return list;
    }

    public void departmentDelete(String id) throws Exception {
        String sql="delete from department where id=?";
        Object[] obj={id};
        addOrupdOrdel(sql,obj);
    }

    public void departmentUpdate(Department department) throws Exception {
        String sql="update department set department_number=? ,name=?,telephone=?,address=?,notes=? where id=?";
        Object[] obj={department.getDepartment_number(),department.getName(),
                    department.getTelephone(),department.getAddress(),department.getNotes(),department.getId()};
        addOrupdOrdel(sql,obj);
    }
    public List<Department> updateList(String id) throws Exception {
        String sql="select * from department where id=?";
        Object[] obj={id};
        List<Department> list = getList(sql, new Department(), obj);
        return list;
    }
    public int departmentAddList() throws Exception {
        String sql="select max(department_number) from department";
        int num = getCount(sql, new Object[0]);
        return num;

    }
    public void departmentAdd(Department department) throws Exception {
        String sql="insert into department (department_number,name,telephone,address,notes) values(?,?,?,?,?)";
        Object[] obj={department.getDepartment_number(),
        department.getName(),
        department.getTelephone(),
        department.getAddress(),
        department.getNotes()};
        addOrupdOrdel(sql,obj);
    }

    public List<Department> getPageList(String pageNo) throws Exception {
        String sql="select * from department limit ?,?";
        int num = (Integer.parseInt(pageNo) - 1) * 5;
        Object[] obj={num,5};
        List<Department> list = getList(sql, new Department(), obj);
        return list;
    }
    public int getPageCount() throws Exception {
        String sql="select count(*) from department";
        int count = getCount(sql, new Object[0]);
        return count;
    }
}
